<template>
  <div class="index">
    <el-carousel :interval="5000" arrow="always">
      <el-carousel-item v-for="(banner, index) in banners" :key="index">
        <el-image style="height: 300px;" :src="banner.url" />
      </el-carousel-item>
    </el-carousel>
    <div class="index-content">
      <div class='center'>
        <h2>热门旅游</h2>
        <p class="center-search">
          <el-input
            v-model="searchValue"
            placeholder="请输入搜索词，比如'广州'"
          />
          <el-button
            class="search-btn"
            type="text"
            icon="el-icon-search"
            @click.stop="searchList"
          />
        </p>
        <p>跟团游、本地游、个人游等各种热门最新旅游推荐</p>
      </div>
      <el-row type="flex" class="search-tab">
        <span
          v-for="(item, index) in options"
          :key="index"
          :class="{ active: index === currentOption }"
          @click="handleOption(index, item)"
        >
          <i>{{ item.name }}</i>
        </span>
      </el-row>
      <div class="search-list">
        <div v-for="(item, index) in list" class="item" :key="index">
          <nuxt-link :to="`/order/detail?id=${item.ts_id}`"><img :src="item.picture ? item.picture : picture" :alt="item.topic"></nuxt-link>
          <p class="item-content">
            <strong class='title'>&lt;{{ item.topic }}&gt;</strong>
            {{ item.details }}
          </p>
          <div>
          <span class='price'>¥ <strong>{{ item.cost }}</strong> 起</span>
          </div>
          <div class="type">{{ item.tourist_type }}</div>
        </div>
      </div>  
      <el-pagination
        class="pagination-bottom"
        layout="prev, pager, next"
        :total="total"
        @current-change="changePage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Index',
  data () {
    return {
      searchValue: '',
      query: {
        pageSize: 10,
        currentPage: 1,
        type: '跟团游'
      },
      total: 1,
      banners: [
        {
          url: require('../assets/images/information.jpg')
        },
        {
          url: require('../assets/images/information2.jpg')
        },
        {
          url: require('../assets/images/information3.jpg')
        },
        {
          url: require('../assets/images/information4.jpg')
        }
      ],
      options: [      // 搜索框tab选项
        {
          name: '跟团游'
        },
        {
          name: '本地游'
        },
        {
          name: '个人游'
        }
      ],
      searchValue: '',    // 搜索框的值
      currentOption: 0,   // 当前选中的选项
      picture: require('../assets/images/tour1.jpg'),
      list: [
        {
          picture: require('../assets/images/tour1.jpg'),
          topic: '曼谷-芭提雅6日游',
          details: '包团特惠，超丰富景点，升级1晚国五，无自费，更赠送600元/成人自费卷',
          cost: 2865,
          tourist_type: '跟团游',
          ts_id: 1,
        },
        {
          picture: require('../assets/images/tour2.jpg'),
          topic: '瑞典6日游',
          details: '包团特惠，超丰富景点，升级1晚国五，无自费，更赠送600元/成人自费卷',
          cost: 2865,
          tourist_type: '跟团游',
          ts_id: 2,
        }
      ]
    }
  },
  methods: {
    async searchList() {
      const { currentPage, pageSize, type } = this.query
      let currentType
      switch(type) {
        case '跟团游':
          currentType = 1
          break
        case '本地游':
          currentType = 2
          break
        case '个人游':
          currentType = 3
          break
      }
      const [err, res] = await this.$api.searchToutistList({
        content: this.searchValue,
        currentPage,
        pageSize,
        type:currentType
      })
      if (err) {
        err.msg = '获取数据失败'
        console.log(err)
        return
      }
      this.list = [...res.data.list]
      this.total = res.data.total
    },
    async getList() {
      const [err, res] = await this.$api.getSelectTouristList(this.query)
      if (err) {
        err.msg = '获取数据失败'
        console.log(err)
        return
      }
      this.list = [...res.data.list]
      this.total = res.data.total
    },
    changePage(page) {
      this.query.currentPage = page
      this.getList()
    },
    handleOption (index, item) {
      this.currentOption = index
      this.query.currentPage = 1
      this.query.type = item.name
      this.getList()
    },
    handleSearch () {
      const optionItem = this.options[this.currentOption]
      // 跳转时候给对应的页面url加上搜索内容参数
      this.$router.push(optionItem.pageUrl + this.searchValue)
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
$linkColor: #ffa500;
.index-content {
  width:1263px;
  // height:1130px;
  margin: 10px auto 80px;
  position: relative;
  .center {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-content: center;
    text-align: center;
    h2 {
      font-size: 40px;
      letter-spacing: 2px;
      color:#666;
      margin:10px 0;
    }
    p {
      color:#888;
      margin:10px 0;
    }
    .center-search {
      position: relative;
      width: 300px;
      margin: 0 auto;
      .el-input {
        ::v-deep .el-input__inner {
          border: 3px solid $linkColor;
          border-radius: 0;
          &:focus {
            border-color: $linkColor;
          }
        }
      }
      .search-btn {
        position: absolute;
        top: 50%;
        right: 0;
        margin-right: 10px;
        transform: translateY(-50%);
        font-size: 24px;
        color: $linkColor;
        ::v-deep .el-icon-search {
          font-weight: bold;
        }
      }
    }
  }
  .search-list {
    .item {
      display: inline-block;
      width: 380px;
      border:1px solid #ddd;
      border-radius:4px;
      margin:15px 12px;
      padding:4px;
      position: relative;
      img {
        /*去掉图片与容器间的缝隙，具体为啥能去掉不知道*/
        vertical-align: middle;
        height: 263px;
        width: 380px;
      }
      .item-content {
        color:#777;
        font-size: 14px;
        padding:7px 0 5px 0;
        letter-spacing: 1px;
        .title {
          color:#333;
          font-style: normal;
        }
      }
      .sat {
        float: right;
        color:#999;
        font-style: normal;
        font-size: 14px;
      }
      .price {
        color:#f60;
        strong {
          font-size: 20px;
          letter-spacing:1px;
        }
      }
      .type{
        display: inline-block;
        width: 90px;
        height: 25px;
        line-height: 25px;
        font-style: normal;
        background-color:#59b200;
        letter-spacing: 1px;
        text-align: center;
        color:#fff;
        position: absolute;
        top:4px;
        left: 4px;
      }
    }
  }
 
  .search-tab {
    margin-left: 12px;
    .active {
      i {
        color: #333;
      }
      &::after {
        background: #eee;
      }
    }

    span {
      width: 82px;
      height: 36px;
      display: block;
      position: relative;
      margin-right: 8px;
      cursor: pointer;

      i {
        position: absolute;
        z-index: 2;
        display: block;
        width: 100%;
        height: 100%;
        line-height: 30px;
        text-align: center;
        color: #fff;
      }
      &:after {
        position: absolute;
        left: 0;
        top: 0;
        display: block;
        content: "";
        width: 100%;
        height: 100%;
        border: 1px rgba(255, 255, 255, 0.2) solid;
        border-bottom: none;
        transform: scale(1.1, 1.3) perspective(0.7em) rotateX(2.2deg);
        transform-origin: bottom left;
        background: rgba(0, 0, 0, 0.5);
        border-radius: 1px 2px 0 0;
        box-sizing: border-box;
      }
    }
  }
  .pagination-bottom {
    position: absolute;
    left: 50%;
    bottom: -40px;
    transform: translateX(-50%);
  }
}
</style>
